package com.music.word;

import com.music.dao.CmUserDaoMapper;
import com.music.entity.CmUser;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

@Service
public class WriteDoc {

    @Autowired
    CmUserDaoMapper cmUserDaoMapper;

    public void exportDoc(){
        List<CmUser> all = cmUserDaoMapper.findAll();
        List s=new ArrayList();
        for (CmUser cmUser : all) {
            s.add(cmUser.getuDesc());
        }
        //创建Word对象
        XWPFDocument xwpfDocument = new XWPFDocument ();
        //创建一个段落对象
        XWPFParagraph titleParagraph = xwpfDocument.createParagraph ();
        //创建文本对象
        XWPFRun titleParagraphRun = titleParagraph.createRun ();
        titleParagraphRun.setBold (true);
//        titleParagraphRun.setText ("这是一个POI操作Word的测试");
        titleParagraphRun.setColor ("FF0000");
        titleParagraphRun.setText (s.toString());
        String fileName = "POI操作Word.doc";
        String dirPath="E:\\poi\\word\\";
        try {
            File file = new File(dirPath);
            if (!file.exists()) {
                file.mkdirs();
            }
            OutputStream outputStream = new FileOutputStream (dirPath+fileName);
            //执行写出到本地
            xwpfDocument.write (outputStream);
            xwpfDocument.close ();
        } catch (FileNotFoundException e) {
            e.printStackTrace ();
        } catch (IOException e) {
            e.printStackTrace ();
        }
    }
}